package com.watchthelan.repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.watchthelan.domain.Event;

public class JdbcEventDao extends SimpleJdbcDaoSupport implements EventDao {
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());


	@Override
	public void addEvent(Event event) {
		logger.info("adding event");
		int id = getSimpleJdbcTemplate().queryForInt("select max(id) from events", new HashMap<String, String>()) + 1;
		logger.info("next id: " + id);
		int count = getSimpleJdbcTemplate().update(
				"insert into events (id, name, description, owner, server, minute, hour, day, month) values(:id, :name, :description, :owner, :server, :minute, :hour, :day, :month)",
				new MapSqlParameterSource().addValue("id", id)
				.addValue("name", event.getName())
				.addValue("description", event.getDescription())
				.addValue("owner", event.getAdministrator())
				.addValue("server", event.getServer())
				.addValue("minute", event.getMinute())
				.addValue("hour", event.getHour())
				.addValue("day", event.getDay())
				.addValue("month", event.getMonth()));
		logger.info("Rows affected: " + count);
	}

	@Override
	public void deleteEventById(long id) {
		logger.info("deleting event with id " + id);
		int count = getSimpleJdbcTemplate().update("delete from events where id=:id", new MapSqlParameterSource().addValue("id", id));
		logger.info("Rows affected: " + count);
	}

	@Override
	public Event getEventById(long id) {
		logger.info("get event by id");
		Event event = getSimpleJdbcTemplate().query("select * from events where id=?", 
				new ParameterizedRowMapper<Event>() {
            		public Event mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
            			Event e = new Event();
            			e.setName(rs.getString("name"));
            			e.setDescription(rs.getString("description"));
            			e.setAdministrator(rs.getInt("owner"));
            			e.setServer(rs.getInt("server"));
            			e.setMinute(rs.getInt("minute"));
            			e.setHour(rs.getInt("hour"));
            			e.setDay(rs.getInt("day"));
            			e.setMonth(rs.getInt("month"));
            			return e;
            }
        }, id).get(0);
		event.setId(id);
		logger.info("got event: " + event);
		return event;
	}

	@Override
	public List<Event> getEvents() {
		logger.info("get events");
		List<Event> events = getSimpleJdbcTemplate().query("select id, name, description, owner, server, minute, hour, day, month from events", new EventRowMapper());		
		return events;
	}
	
	public List<Event> getEventsForServer(Long id) {
		List<Event> events = getSimpleJdbcTemplate().query("select id, name, description, owner, server, minute, hour, day, month from events " +
				"where server=?", new EventRowMapper(), id);
		return events;
	}

	@Override
	public void updateEvent(Event event) {
		logger.info("updating event: " + event);
		int count = getSimpleJdbcTemplate().update(
				"update events set id=:id, name=:name, description=:description, owner=:owner, server=:server, minute=:minute, hour=:hour, day=:day, month=:month  where id=:id",
				new MapSqlParameterSource().addValue("id", event.getId())
				.addValue("name", event.getName())
				.addValue("description", event.getDescription())
				.addValue("owner", event.getAdministrator())
				.addValue("server", event.getServer())
				.addValue("minute", event.getMinute())
				.addValue("hour", event.getHour())
				.addValue("day", event.getDay())
				.addValue("month", event.getMonth()));
		logger.info("Rows affected: " + count);
	}
	
	private static class EventRowMapper implements ParameterizedRowMapper<Event> {

		public Event mapRow(ResultSet rs, int arg1) throws SQLException {
			Event event = new Event();
			event.setId(rs.getLong("id"));
			event.setName(rs.getString("name"));
			event.setDescription(rs.getString("description"));
			event.setAdministrator(rs.getLong("owner"));
			event.setServer(rs.getLong("server"));
			event.setMinute(rs.getInt("minute"));
			event.setHour(rs.getInt("hour"));
			event.setDay(rs.getInt("day"));
			event.setMonth(rs.getInt("month"));
			return event;
		}
	}
}
